﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Managers {
    import flash.events.*;
    import flash.utils.*;
    import flash.net.*;
    import com.adobe.serialization.json.*;
    import com.adobe.crypto.*;
    import flash.external.*;

    public class StatsManager {

        public static const ONE_LINE_FEED:int = 1;
        public static const SHORT_FEED:int = 2;
        public static const APP_TO_USER_NOTIF:int = 3;
        public static const USER_TO_USER_NOTIF:int = 4;
        public static const STATUS:int = 5;
        public static const APP_EMAIL:int = 6;
        public static const REQUEST:int = 7;
        public static const FEED:int = 8;
        private static const REPORT_DELAY:int = 10000;
        private static const IDLE_TIMER_ENABLED:Boolean = true;
        private static const HEARTBEAT_STATS_ENABLED:Boolean = false;

        public static var m_stats:Array = new Array();
        public static var m_timer:Timer = null;
        protected static var m_idle:Boolean = false;
        private static var m_enabled:Boolean = true;
        private static var sample_id:Number = -1;

        protected static function addStatDataToBatch(_arg1:String, ... _args):void{
            if (m_timer == null){
                m_timer = new Timer(REPORT_DELAY);
                m_timer.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
                m_timer.start();
            };
            m_stats.push({
                statfunction:_arg1,
                data:_args
            });
        }
        public static function count(_arg1:String, _arg2:String="", _arg3:String="", _arg4:String="", _arg5:String="", _arg6:String="", _arg7:int=1):void{
            GlobalEngine.msg("StatsManager.count", _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7);
            addStatDataToBatch("count", _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7);
        }
        public static function economy(_arg1:int, _arg2:String, _arg3:String="", _arg4:String="", _arg5:String="", _arg6:String="", _arg7:String="", _arg8:int=1):void{
            addStatDataToBatch("economy", _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8);
        }
        public static function trackMessage(_arg1:int, _arg2:String, _arg3:int, _arg4:String="", _arg5:String="", _arg6:String="", _arg7:Boolean=true):void{
            addStatDataToBatch("trackMessage", _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7);
        }
        public static function sample(_arg1:int, _arg2:String, _arg3:String="", _arg4:String="", _arg5:String="", _arg6:String="", _arg7:String="", _arg8:int=1):void{
            if (willSampleGetRecorded(_arg1)){
                addStatDataToBatch("sample", _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8);
            };
        }
        private static function willSampleGetRecorded(_arg1:int, _arg2:Boolean=true):Boolean{
            var userInfo:* = null;
            var rate:* = _arg1;
            var sampleByUser:Boolean = _arg2;
            if (sampleByUser){
                if (sample_id < 0){
                    try {
                        userInfo = ExternalInterface.call("getUserInfo");
                        sample_id = userInfo.zid;
                    } catch(e:Error) {
                        sample_id = 0;
                    };
                };
                if ((sample_id % rate) == 1){
                    return (true);
                };
                return (false);
            };
            return (true);
        }
        public static function perf(_arg1:String="", _arg2:String="", _arg3:String="", _arg4:String="", _arg5:String="", _arg6:String="", _arg7:String=""):void{
            addStatDataToBatch("perf", _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7);
        }
        public static function perfSample(_arg1:int, _arg2:String="", _arg3:String="", _arg4:String="", _arg5:String="", _arg6:String="", _arg7:String="", _arg8:String=""):void{
            if (willSampleGetRecorded(_arg1)){
                addStatDataToBatch("perfSample", _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8);
            };
        }
        public static function social(_arg1:String, _arg2:String, _arg3:String="", _arg4:String="", _arg5:String="", _arg6:String="", _arg7:int=0, _arg8:String=""):void{
            GlobalEngine.msg("StatsManager.social", _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8);
            addStatDataToBatch("social", _arg1, _arg2, _arg3, _arg4, _arg5, _arg6, _arg7, _arg8);
        }
        public static function startExperiment(_arg1:String, _arg2:String, _arg3:int):int{
            var _local4:int;
            var _local5 = 7;
            var _local6:String = MD5.hash((_arg2 + _arg1));
            var _local7:int = parseInt(("0x" + _local6.substr(-(_local5))));
            _local4 = (_local7 % (_arg3 + 1));
            addStatDataToBatch("setUserExperimentVariant", _arg1, _arg2, _local4);
            return (_local4);
        }
        public static function experimentGoal(_arg1:String, _arg2:String, _arg3:int=0):void{
            addStatDataToBatch("experimentGoal", _arg1, _arg2, _arg3);
        }
        public static function notIdle():void{
            m_idle = false;
        }
        public static function sendStats(_arg1:Boolean=false):void{
            var _local2:Object;
            var _local3:Object;
            var _local4:URLRequest;
            var _local5:URLLoader;
            if (((m_stats.length) && ((m_enabled == true)))){
                _local2 = new Object();
                _local2.signedParams = TransactionManager.additionalSignedParams;
                _local2.stats = m_stats;
                _local3 = JSON.encode(_local2);
                _local4 = new URLRequest(Config.RECORD_STATS_PATH);
                _local4.method = URLRequestMethod.POST;
                _local4.contentType = "application/json";
                _local4.data = _local3;
                _local5 = new URLLoader();
                _local5.dataFormat = URLLoaderDataFormat.BINARY;
                _local5.load(_local4);
                m_stats = [];
            };
        }
        private static function onTimer(_arg1:TimerEvent):void{
            if (HEARTBEAT_STATS_ENABLED){
                if (((m_idle) && (IDLE_TIMER_ENABLED))){
                    count("timer", "idle");
                } else {
                    count("timer", "active");
                };
            };
            sendStats();
            m_idle = true;
        }
        public static function milestone(_arg1:String, _arg2:Number=0):void{
            addStatDataToBatch("milestone", _arg1, _arg2);
        }
        public static function disable():void{
            m_enabled = false;
        }
        public static function enable():void{
            m_enabled = true;
        }

    }
}//package Engine.Managers 
